package com.own.component.common.base.open.api;

import com.own.component.common.base.entity.BaseBo;
import com.own.component.common.base.entity.BaseMap;
import com.own.component.common.base.entity.BaseQuery;
import com.own.component.common.base.entity.po.BaseBean;
import com.own.component.common.base.entity.vo.BaseBeanVo;
import com.own.component.common.base.service.BaseService;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.UpdateModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * BaseOpenMethod
 *
 * @author chenxueli
 * @date 2021/4/21 15:59
 */
@Slf4j
@Component
public abstract class BaseOpenMethod<
        PO extends BaseBean,
        BO extends BaseBo<PO>,
        VO extends BaseBeanVo<PO>,
        MAP extends BaseMap<PO>,
        QUERY extends BaseQuery,
        SERVICE extends BaseService<PO, BO, VO, MAP, QUERY>
        > {

    @SuppressWarnings("all")
    @Autowired
    private SERVICE service;

    /**
     * 查询所有的数据
     *
     * @param query 查询条件
     * @return 所有的列表数据
     */
    public List<VO> all(QUERY query) {
        return service.all(query);
    }

    /**
     * 分页查询信息
     *
     * @param query 查询条件
     * @return 分页数据
     */
    public PageModel<VO> page(QUERY query) {
        return service.page(query);
    }

    /**
     * 根据id获取信息
     *
     * @param pk 主键id
     * @return 查询出的对象
     */
    public VO getById(Long pk) {
        return service.getById(pk);
    }

    /**
     * 添加信息
     *
     * @param bo 添加对象
     * @return 添加标识
     */
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<VO> add(BO bo) {
        return service.add(bo);
    }

    /**
     * 修改信息
     *
     * @param pk 主键id
     * @param bo 修改对象
     * @return 修改标识
     */
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<VO> update(Long pk, BO bo) {
        return service.update(pk, bo);
    }

    /**
     * 删除信息
     *
     * @param pk 主键id
     * @return 删除标识
     */
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<Boolean> delete(Long pk) {
        return service.delete(pk);
    }

}
